package cn.sc.allinpay.allfavour.business.checktrans;

import android.os.Handler;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.sc.allinpay.allfavour.base.db.DbDao;
import cn.sc.allinpay.allfavour.base.model.BaseModel;
import cn.sc.allinpay.allfavour.business.common.data.CheckCouponsRecord;

/**
 * 验券流水
 * author：pany
 * on 2017/12/5 14:44
 */
public class CheckTransModel extends BaseModel {

    private Handler handler;

    public CheckTransModel() {
        this.handler = new Handler();
    }

    /**
     * 查询验券流水
     *
     * @param date     0：今日 1：所有
     * @param callBack
     */
    public void queryTransRecord(final int date, final TransCallBack callBack) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<CheckCouponsRecord> tempTransRecords = null;
                if (date == 1) {//查询所有
                    DbDao<CheckCouponsRecord> dbDao = getDao(CheckCouponsRecord.class);
                    tempTransRecords = dbDao.queryAll();
                } else {
                    DbDao<CheckCouponsRecord> dbDao = getDao(CheckCouponsRecord.class);
                    QueryBuilder<CheckCouponsRecord, Integer> queryBuilder = dbDao.queryBuilder();
                    queryBuilder.orderBy("TRANSTIME", false);
                    Where<CheckCouponsRecord, Integer> where = queryBuilder.where();
                    try {
                        if (date == 0) {//今日
                            String crtDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
                            where.eq("TRANSDATE", crtDate);
                        }
                        tempTransRecords = where.query();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }

                final List<CheckCouponsRecord> transRecords = tempTransRecords;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onResult(transRecords, transRecords != null ? transRecords.size() : 0);
                    }
                });
            }
        }).start();
    }


    public interface TransCallBack {

        /**
         * 验券流水
         *
         * @param resp 回调值
         */
        void onResult(List<CheckCouponsRecord> resp, int size);

    }
}
